function home() {
    location.href = "index.html"
}

function out() {
    sessionStorage.clear()
    if ($('#login').html() == '退出登录') {
        location.href = 'index.html'
    } else {
        location.href = 'login.html'
    }
}

var length
$(function() {
    //获取用户名
    var userinfo = sessionStorage['users']
    if (userinfo == null) {
        $('#userName').html("未登录");
        $('#login').html('登录/注册')
    } else {
        var user = JSON.parse(sessionStorage['users'])
        $('#userName').html(user[0].Name)
        $('#login').html('退出登录')
    }
    var search = sessionStorage['search']
    $.get('data/goods.json', function(response) {
        if (search == null) {
            data = response;
            recordCount = data.goods.length;
            length = recordCount
            pageCount = Math.ceil(recordCount / pageSize);
            initPage();
            initData();
        } else {
            data = response;
            search1()
        }
    })
    $('.classify-row1 li').click(function() {
        if (this.className != 'red') {
            this.classList.add('red')
            $(this).siblings('li').removeClass('red')
        } else {
            this.classList.remove('red')
        }
    })
    $('.classify-row3 li').click(function() {
        if (this.className != 'red') {
            this.classList.add('red')
        } else {
            this.classList.remove('red')
        }
    })
    $('.sort').click(function() {
        function sortprice(a, b) {
            return a.price - b.price
        }
        data.goods.sort(sortprice);
        console.log(data)
        recordCount = data.goods.length;
        pageCount = Math.ceil(recordCount / pageSize);
        initPage();
        initData();

    })

})


//填充json数据
var data = null;
var recordCount = 0; //总记录数
var pageSize = 8; //每页显示的记录数
var pageCount = 0; //总页数
var pageIndex = 0; //当前页索引值
// //初始化分页指示符
function initPage() {
    var ulPages = document.querySelector('.page-ul');
    var strPages = '';
    for (var i = 0; i < pageCount; i++) {
        strPages += `<li onclick="showData(${i})">${i+1}</li>`;
    }
    ulPages.innerHTML = strPages;
}

// //显示当前页数据的方法
function showData(index) {
    pageIndex = index;
    initData();
}

//显示体数据的方法
function initData() {
    var ul = document.querySelector('.goods-ul');
    var strHTML = ``;
    for (var i = pageIndex * pageSize; i < (pageIndex + 1) * pageSize; i++) {
        if (data.goods[i]) {
            strHTML += ` 
            <li onclick="check(this)">
            <img src="${data.goods[i].imgPath}" alt="">
            <p>标题：<span>${data.goods[i].title}</span></p>
            <p>价格：<span>${data.goods[i].price}元</span></p>
        </li>`;
        }
    }
    ul.innerHTML = strHTML;
}

function search() {
    sessionStorage.clear()
    var ul = document.querySelector('.goods-ul');
    var what = $('[type="text"]').val()
    if (what == '') {
        recordCount = length
        pageCount = Math.ceil(recordCount / pageSize);
        initPage()
        initData();
    } else {
        var strHTML = ``;
        var a = 0
        for (var i = pageIndex * pageSize; i < (pageIndex + 1) * pageSize; i++) {
            if (data.goods[i].title.indexOf(what) != -1 || data.goods[i].shop.indexOf(what) != -1 || data.goods[i].message.indexOf(what) != -1) {
                strHTML += ` 
                <li onclick="check(this)">
                <img src="${data.goods[i].imgPath}" alt="">
                <p>标题：<span>${data.goods[i].title}</span></p>
                <p>价格：<span>${data.goods[i].price}元</span></p>
            </li>`
                a++
            }
        }
        ul.innerHTML = strHTML;
        recordCount = a
        pageCount = Math.ceil(recordCount / pageSize);
        initPage1()
    }
}

function search1() {
    var searchT = JSON.parse(sessionStorage['search']);
    var what = searchT[0].search
    var ul = document.querySelector('.goods-ul');
    var strHTML = ``;
    var a = 0
    for (var i = pageIndex * pageSize; i < (pageIndex + 1) * pageSize; i++) {
        if (data.goods[i].title.indexOf(what) != -1 || data.goods[i].shop.indexOf(what) != -1 || data.goods[i].message.indexOf(what) != -1) {
            strHTML += ` 
            <li onclick="check(this)">
            <img src="${data.goods[i].imgPath}" alt="">
            <p>标题：<span>${data.goods[i].title}</span></p>
            <p>价格：<span>${data.goods[i].price}元</span></p>
        </li>`
            a++
        }
    }
    ul.innerHTML = strHTML;
    recordCount = a
    pageCount = Math.ceil(recordCount / pageSize);
    initPage1()
}

function initPage1() {
    var ulPages = document.querySelector('.page-ul');
    var strPages = '';
    for (var i = 0; i < pageCount; i++) {
        strPages += `<li onclick="showData1(${i})">${i+1}</li>`;
    }
    ulPages.innerHTML = strPages;
}

function showData1(index) {
    pageIndex = index;
    search();
}

function check(result) {
    var title = result.children[1].lastElementChild.innerText
    var products = []
    products.push({ "title": title });
    sessionStorage['products'] = JSON.stringify(products);
    location.href = 'good.html'
}